package cn.easier.activity.console.service.impl;

import cn.easier.activity.console.entity.MessagePost;
import cn.easier.activity.console.entity.MessagePostRecord;
import cn.easier.activity.console.params.MessagePostParams;
import cn.easier.activity.console.repository.MessagePostRecordRepository;
import cn.easier.activity.console.repository.MessagePostRepository;
import cn.easier.activity.console.service.MessagePostService;
import cn.easier.activity.console.utils.EmailAndSmsUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MessagePostServiceImpl implements MessagePostService {

    private final MessagePostRepository messagePostRepository;
    private final MessagePostRecordRepository messagePostRecordRepository;
    private final EmailAndSmsUtil emailAndSmsUtil;

    @Autowired
    public MessagePostServiceImpl(MessagePostRepository messagePostRepository, MessagePostRecordRepository messagePostRecordRepository, EmailAndSmsUtil emailAndSmsUtil) {
        this.messagePostRepository = messagePostRepository;
        this.messagePostRecordRepository = messagePostRecordRepository;
        this.emailAndSmsUtil = emailAndSmsUtil;
    }

    @Override
    public void preSend(MessagePostParams params) {
        MessagePost messagePost = new MessagePost();
        messagePost.setName(params.getName());
        messagePost.setNum(params.getTarget().size());
        messagePost.setSmsContent(params.getSmsContent());
        messagePost.setEmailContent(params.getEmailContent());
        String time = params.getTime();
        Date date = new Date();
        if (StringUtils.isNotBlank(time)) {
            try {
                date = DateUtils.parseDate(time, "yyyy-MM-dd HH:mm:ss");
            } catch (ParseException ignored) {
            }
        }
        messagePost.setPostTime(date);
        messagePost.setStatus(1); // 未投递
        messagePost.setType(params.getType());
        messagePost.setPostType(params.getPostType());
        messagePost.setEnv(params.getEnv());
        messagePost.setRemark("");
        messagePost.setCreateTime(new Date());
        messagePost.setEmailTemplateCode(params.getEmailTemplateCode());
        messagePost.setSmsTemplateCode(params.getSmsTemplateCode());
        messagePost.setCreator(params.getCreator());
        Integer id = messagePostRepository.save(messagePost).getId();
        params.setId(id);
    }

    @Async
    @Override
    public void send(MessagePostParams params) {
        if (params.getId() == null) {
            preSend(params);
        }
        MessagePost messagePost;
        List<String> failList;
        if (params.getType() == 1) {
            messagePost = messagePostRepository.findOne(params.getId());
            failList = emailAndSmsUtil.sendEmailAndReturnFail(params.getTarget(), params.getEmailTemplateCode(), params.getEmailContent(), null);
        } else if (params.getType() == 3) {
            messagePost = messagePostRepository.findOne(params.getId());
            failList = emailAndSmsUtil.sendEmailAndSmsAndReturnFail(params.getTarget(), params.getEmailTemplateCode(), params.getSmsTemplateCode(), params.getEmailContent(), params.getSmsContent(), null);
        } else {
            return;
        }
        List<MessagePostRecord> list = new ArrayList<>();
        for (String email : params.getTarget()) {
            MessagePostRecord record = new MessagePostRecord();
            record.setPostId(messagePost.getId());
            record.setTarget(email);
            record.setStatus(failList.contains(email) ? 2 : 1);
            record.setRemark("");
            list.add(record);
        }
        if (failList.size() == params.getTarget().size()) {
            messagePost.setStatus(3); // 投递失败
        } else {
            messagePost.setStatus(2); // 投递成功
            messagePostRecordRepository.save(list);
        }
        messagePostRepository.save(messagePost);
    }
}
